// pages/renzheng/renzheng.js
var userInfo;
const DB = wx.cloud.database()
var util = require('../../utils/util.js')
var app = getApp()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    if (wx.getStorageSync('userInfo')) {
      getApp().DB.collection('user').doc(wx.getStorageSync('userInfo')._id).get().then(res => {
        console.log('res', res.data)
        this.setData({
          userInfo: res.data,
          ...res.data.userInfoDetail
        })
      })
    }
  },
  tijiao() {
    console.log('提交')
    var userInfoDetail = that.data.userInfo.userInfoDetail;
    if (!this.data.zhengmian && !this.data.fanmian && !this.data.xingshizheng && !this.data.fangcanzheng && !this.data.xuelizheng) {
      wx.showToast({
        title: '请全部上传',
        icon: 'none'
      })
      return
    }
    if (this.data.zhengmian) userInfoDetail.zhengmian = this.data.zhengmian;
    if (this.data.fanmian) userInfoDetail.fanmian = this.data.fanmian;
    if (this.data.xingshizheng) userInfoDetail.xingshizheng = this.data.xingshizheng;
    if (this.data.fangcanzheng) userInfoDetail.fangcanzheng = this.data.fangcanzheng;
    if (this.data.xuelizheng) userInfoDetail.xuelizheng = this.data.xuelizheng;
    DB.collection('user').doc(that.data.userInfo._id).update({
      data: {
        userInfoDetail: userInfoDetail
      },
      success: function (res) {
        console.log('2222222222222')
        that.senMsg(0)
        console.log('q发送')
        wx.showToast({
          title: '上传成功',
          icon: 'none'
        })

        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      },
      fail: function (err) {

        console.log('3333333333', err)
      }
    })
  },

  senMsg(idx) {
    console.log('打算发送消息')

    var data = {
      openid: getApp().guanli_list_openid[idx],
      page: "pages/my/index",
      date4: util.formatTime(new Date()),
      thing1: '认证审核',
      phrase2: "身份认证",
      name3: that.data.userInfo.userInfoDetail.nicheng,
      type: 3,
      templateId: "bzyl7or1NmoyNJ_OEajl0I9WBpJ7idMLgqWjsjqS7RU",
      miniprogramState: "developer"
    }
    // developer为开发版；trial为体验版；formal为正式版；默认为正式版
    wx.cloud.callFunction({
      name: "sendMsg",
      data: data,
      success(res) {
        console.log('发送模板消息成功-------------', res.result)
        if (idx < getApp().guanli_list_openid.length - 1) {
          idx = idx + 1
          that.senMsg(idx)
        }
      },
      fail(err) {
        console.log('发送模板消息失败-------------', err)

        if (idx < getApp().guanli_list_openid.length - 1) {
          idx = idx + 1
          that.senMsg(idx)
        }
      }
    })
  },
  zhengmian_img() {
    this.setImg().then(res => {
      this.setData({
        zhengmian: res
      })
    })
  },
  fanmian_img() {
    this.setImg().then(res => {
      this.setData({
        fanmian: res
      })
    })
  },
  xingshizheng_img() {
    this.setImg().then(res => {
      this.setData({
        xingshizheng: res
      })
    })
  },
  fangcanzheng_img() {
    this.setImg().then(res => {
      this.setData({
        fangcanzheng: res
      })
    })
  },
  xuelizheng_img() {
    this.setImg().then(res => {
      this.setData({
        xuelizheng: res
      })
    })
  },
  setImg() {
    let that = this
    var ctx = wx.createCanvasContext('myCanvas')
    console.log('1111111')
    return new Promise(rrr => {
      console.log('22222')

      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          console.log('333333333', res.tempFilePaths[0])
          //获取图片基本信息
          wx.getImageInfo({
            src: res.tempFilePaths[0],
            success: function (res) {
              var width = res.width
              console.log('44444', width)
              var height = res.height
              //获取屏幕宽度
              let screenWidth = wx.getSystemInfoSync().windowWidth
              //处理一下图片的宽高的比例
              if (width >= height) {
                if (width > screenWidth) {
                  width = screenWidth
                }
                height = height / res.width * width
              } else {
                if (width > screenWidth) {
                  width = screenWidth
                }
                if (height > 400) {
                  height = 400
                  width = res.width / res.height * height
                } else {
                  height = height / res.width * width
                }
              }
              that.setData({
                imageWidth: width,
                imageHeight: height,
              })
              ctx.drawImage(res.path, 0, 0, width, height)
              ctx.rotate(20 * Math.PI / 180)
              for (let j = 1; j < 12; j++) {
                ctx.beginPath()
                ctx.setFontSize(14)
                ctx.setFillStyle('white')
                ctx.fillText('仅供游友侣行实名认证使用', 0, 50 * j)
                for (let i = 1; i < 12; i++) {
                  ctx.beginPath()
                  ctx.setFontSize(14)
                  ctx.setFillStyle('white')
                  ctx.fillText("仅供游友侣行实名认证使用", (15 + (14 - 1) * "仅供游友侣行实名认证使用".length) * i, 50 * j)
                }
              }

              console.log('55555')
              ctx.draw(false, () => {
                console.log('66666')
                //生成图片
                wx.canvasToTempFilePath({
                  canvasId: 'myCanvas',
                  success: function (res) {
                    console.log("canvas可以生成图片")
                    console.log(res.tempFilePath, 'canvas图片地址');

                    // tempFilePath可以作为img标签的src属性显示图片
                    console.log('选择图片', res)
                    const tempFilePaths = res.tempFilePath
                    wx.cloud.uploadFile({
                      cloudPath: '11renzhengImg/' + Date.parse(new Date()) + ".jpg",
                      filePath: tempFilePaths,
                      success: res => {
                        console.log('上传成功', res)
                        let imgUrl = res.fileID
                        rrr(imgUrl)
                      },
                      fail: err => {
                        console.log('上传失败', err)
                      }
                    })
                  }
                })
              })

            }
          })
        }
      })
    })
  },
})